Qick Question about the PyQT tutorials

by: mdonovan, 7 years ago


Hi again ... I ran through your PyQT tutorials ... really nice ... My question is in regards to the run function.

def run():    
    app = QtWidgets.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())


in Eric I get a warning -
Warning : Local Variable 'GUI' is assigned to but never used.

So my question is ... Is there a better/alternative way of doing this ? For anyone that is attempting these tutorials using PyQT5 ... here is the code... there are some small changes from PyQT4 that require some adjustments.

import sys
from PyQt5 import QtCore,  QtGui,  QtWidgets

class Window (QtWidgets.QMainWindow) :
    
    def __init__(self):
        super (Window,  self).__init__()
        self.setGeometry(50, 50, 500, 300)
        self.setWindowTitle("PyQT Tutorial")
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))
        
        extractAction = QtWidgets.QAction("&GET TO THE CHOPPA",  self)
        extractAction.setShortcut("Ctrl+Q")
        extractAction.setStatusTip("Leave the App")
        extractAction.triggered.connect(self.close_application)
        
        self.statusBar()
    
        openEditor = QtWidgets.QAction("&Editor",  self)
        openEditor.setShortcut("Ctrl-E")
        openEditor.setStatusTip('Open Editor')
        openEditor.triggered.connect(self.editor)
    
        openFile = QtWidgets.QAction("&Open File",  self)
        openFile.setShortcut("Ctrl-O")
        openFile.setStatusTip('Open File')
        openFile.triggered.connect(self.file_open)
    
        saveFile = QtWidgets.QAction("&Save File",  self)
        saveFile.setShortcut("Ctrl-S")
        saveFile.setStatusTip('Save File')
        saveFile.triggered.connect(self.file_save)
    
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(extractAction)
        fileMenu.addAction(openFile)
        fileMenu.addAction(saveFile)
        
        editorMenu = mainMenu.addMenu("&Editor")
        editorMenu.addAction(openEditor)
        
        self.home()
        
    def home(self):
        btn  = QtWidgets.QPushButton('Quit',  self)
        btn.clicked.connect(self.close_application)      
        btn.resize (btn.minimumSizeHint())
        btn.move (0, 100)
        
        extractAction = QtWidgets.QAction(QtGui.QIcon('Sheep.png'), 'Flee the Scene',self)
        extractAction.triggered.connect(self.close_application)
        self.toolBar = self.addToolBar("Extraction")
        self.toolBar.addAction(extractAction)
        
        fontChoice = QtWidgets.QAction('Font',self)
        fontChoice.triggered.connect(self.font_choice)
        #self.toolBar = self.addToolBar("Font")
        self.toolBar.addAction(fontChoice)
        
        #color = QtGui.QColor(0, 0, 0)
        
        fontColor = QtWidgets.QAction('Font BG Color',  self)
        fontColor.triggered.connect(self.color_picker)
        
        self.toolBar.addAction(fontColor)
        
        checkBox = QtWidgets.QCheckBox('Enlarge Window',  self)
        checkBox.move(300,  25)
        checkBox.stateChanged.connect(self.enlarge_window)
    
        self.progress = QtWidgets.QProgressBar(self)
        self.progress.setGeometry(200, 80, 250,  20)
    
        self.btn2 = QtWidgets.QPushButton('download',  self)
        self.btn2.move(200, 120)
        self.btn2.clicked.connect(self.download)
    
        #print (self.style().objectName())
        self.styleChoice = QtWidgets.QLabel('Windows Vista',  self)
    
        comboBox = QtWidgets.QComboBox(self)
        comboBox.addItem("motif")
        comboBox.addItem("Windows")
        comboBox.addItem("cde")
        comboBox.addItem("Plastique")
        comboBox.addItem("Cleanlooks")
        comboBox.addItem("windowsvista")
        
        comboBox.move(50, 250)
        self.styleChoice.move (50, 150)
        comboBox.activated[str].connect(self.style_choice)
        
        cal = QtWidgets.QCalendarWidget(self)
        cal.move(500, 200)
        cal.resize(200, 200)
        
        self.show()
      
    def file_open(self):
        name = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File')
#        print ("this is a tuple: %s" % (name,))
#        print (name)
        file = open(name[0],'r')
        self.editor()

        with file:
            text = file.read()
            self.textEdit.setText(text)
    
    def file_save(self):
        name = QtWidgets.QFileDialog.getSaveFileName(self, 'Save File')
        file = open(name[0],'w')
        text = self.textEdit.toPlainText()
        file.write(text)
        file.close()
        
    def color_picker(self):
        color = QtWidgets.QColorDialog.getColor()
        self.styleChoice.setStyleSheet("QWidget { background-color: %s}" % color.name() )
        
    def editor(self):
        self.textEdit = QtWidgets.QTextEdit()
        self.setCentralWidget(self.textEdit)
        
    def font_choice(self):
        font,  valid = QtWidgets.QFontDialog.getFont()
        if valid :
            self.styleChoice.setFont(font)
    
    def style_choice(self,  text):
        self.styleChoice.setText(text)
        QtWidgets.QApplication.setStyle(QtWidgets.QStyleFactory.create(text))
    
    def download(self):
        self.completed = 0
        
        while self.completed < 100:
            self.completed += 0.0001
            self.progress.setValue(self.completed)
            
    def enlarge_window(self,  state):
        if state == QtCore.Qt.Checked:
            self.setGeometry(50, 50, 1000, 600)
        else:
            self.setGeometry(50, 50, 500, 300)
    
    
    def close_application(self):
#        print('sooo custom!')
        choice = QtWidgets.QMessageBox.question(self,  'Extract!','get into the chopper ?',
                                                                    QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
        if choice  == QtWidgets.QMessageBox.Yes:
#            print("Extracting Now")
            sys.exit()
        else:
            pass
        
def run():    
    app = QtWidgets.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())
    
run()    





You must be logged in to post. Please login or register an account.



Right now, we're just not doing anything or referencing anything from the Window()'s returned data, but yet we're storing it to the GUI variable.

It's fine, it's just your editor telling you that you defined a variable that you aren't using, that's all.

-Harrison 7 years ago

You must be logged in to post. Please login or register an account.